﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Three
{
    enum SnakeDirection
    { 
        UP,DOWN,LEFT,RIGHT
    }

    internal class Snake : IDraw
    {
        SnakeBody[] body;

        int cnt = 0;

        //方向
        SnakeDirection dir;
        public Snake()
        {
            body = new SnakeBody[200];

            body[0] = new SnakeBody(E_SnakeType.HEAD, 40, 10);
            cnt++;

            dir = SnakeDirection.RIGHT;
        }

        public void Draw()
        {
            for (int i = 0; i < cnt; i++)
            {
                body[i].Draw();
            }
        }

        public void Move()
        { 
            //清除最后一个位置
            SnakeBody lastBody = body[cnt - 1];
            Console.SetCursorPosition(lastBody.pos.x, lastBody.pos.y);
            Console.Write("  ");

            for (int i = cnt - 1; i > 0; i--)
            {
                body[i].pos = body[i - 1].pos;
            }

            //自动移动
            switch (dir)
            {
                case SnakeDirection.UP:
                    body[0].pos.y--;
                    break;
                case SnakeDirection.DOWN:
                    body[0].pos.y++;
                    break;
                case SnakeDirection.LEFT:
                    body[0].pos.x -= 2;
                    break;
                case SnakeDirection.RIGHT:
                    body[0].pos.x += 2;
                    break;
                default:break;
            }
        }

        public void ChangeDir(SnakeDirection nextDir)
        {
            //如果有身体情况下，不能朝相反方向移动
            //与当前方向相同也返回
            if (nextDir == dir||cnt>1&&
                (dir==SnakeDirection.LEFT&&nextDir==SnakeDirection.RIGHT||
                dir == SnakeDirection.RIGHT && nextDir == SnakeDirection.LEFT||
                dir == SnakeDirection.UP && nextDir == SnakeDirection.DOWN||
                dir == SnakeDirection.DOWN && nextDir == SnakeDirection.UP))
            {
                return;
            }

            dir = nextDir;
        }

        public bool CheckEnd()
        {
            //头是否碰墙
            if (body[0].pos.x < 2 || body[0].pos.x >= Game.w - 2 || body[0].pos.y < 1 ||
                body[0].pos.y >= Game.h - 2)
            {
                return true;
            }
            //头是否碰身体
            for (int i = 1; i < cnt; i++)
            {
                if (body[i].pos == body[0].pos)
                {
                    return true;
                }
            }

            return false;
        }

        public bool CheckSamePos(Position p)
        {
            for (int i = 0; i < cnt; i++)
            {
                if (p == body[i].pos)
                {
                    return true;
                }
            }

            return false;
        }

        public void CheckEatFood(Food food)
        {
            if (body[0].pos == food.pos)
            {
                AddBody();
                food.RandomPos();
            }
            
        }

        void AddBody()
        {
            body[cnt] = new SnakeBody(E_SnakeType.BODY, body[cnt-1].pos.x, body[cnt-1].pos.y);
            cnt++;
        }

        public void Reset()
        {
            cnt = 0;
            body = new SnakeBody[200];
            body[0] = new SnakeBody(E_SnakeType.HEAD, 40, 10);
            cnt++;
        }
    }
}
